import $ from 'jquery';
export function guid() {
    function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
export const table = (row, col) => {
    let tbody = `<div data-type="blockcard" contenteditable=false data-tag="table"><table  contenteditable=true>\n`
    for (let i = 0; i < row; i++) {
        tbody += "<tr>\n"
        for (let j = 0; j < col; j++) {
            if (i == 0) {
                tbody += `<td style="width:${(100 / col).toFixed(2)}%"></td>\n`
            } else {
                tbody += `<td></td>\n`
            }
        }
        tbody += "</tr>\n"
    }
    tbody += "</tr>\n</table></div>"
    return tbody
}

export const title = (num, text) => `<div data-id="${guid()}" data-type="block" data-tag="title${num}"><h${num}>${text}</h${num}></div>`;

export const input = `<div data-type="inline" contenteditable=false  data-id="${guid()}"  data-tag="input"><input type="text" value="a" readonly/></div>`;

export const p = (text) => `<div data-type="blockcard" data-tag="p"><p>${text}</p></div>`;

export const br = `<br>`;


export const createEle = (type, ...args) => {
    let domR
    switch (type) {
        case 'table':
            domR = $(table(...args))[0]
            break;

        case 'input':
            domR = $(input)[0]
            break;

        case 'br':
            domR = $(br)[0]
            break;

        case 'p':
            let t = args.length > 0 ? args[0] : "";
            domR = $(p(t))[0];
            break;
        case 'title':
            if (args.length < 2) {
                throw new Error('参数要不小于2个');
            }
            domR = $(title(args[0], args[1]))[0];
            break;

        default:
            break;
    }

    return domR;

}